package com.kxf.alarmmanagertest

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.SystemClock
import android.util.Log

class MyAlarmManager(val context: Context) {

    var alarmManager: AlarmManager

    val requestCodeAlarmManagerExact = 101
    val requestCodeAlarmManagerWindow = 102
    val requestCodeAlarmManagerAndAllowWhileIdle = 103
    val requestCodeAlarmManagerExactAndAllowWhileIdle = 104

    lateinit var pendingIntentExact : PendingIntent
    lateinit var pendingIntentWindow : PendingIntent
    lateinit var pendingIntentAndAllowWhileIdle : PendingIntent
    lateinit var pendingIntentExactAndAllowWhileIdle : PendingIntent


    companion object {// 包裹范围内 属于静态方法
        val TAG = "MyAlarmManager"
        val delayTime = 5000
        val ALARM_ACTION_CODE = "MY_ALARM_ACTION_CODE_STATIC"
    }

    init {
        alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

        Log.i(TAG, "MyReceiver::class.java.getPackage().name===>>>" + MyReceiver::class.java.getPackage().name)
        Log.i(TAG, "MyReceiver::class.java.name===>>>" + MyReceiver::class.java.name)

        initAlarmManagerExact()
        initAlarmManagerWindow()
        initAlarmManagerAndAllowWhileIdle()
        initAlarmManagerExactAndAllowWhileIdle()
    }

    fun creatIntentAlarmManager(type : Int):Intent{
        var intentAlarmManager = Intent(ALARM_ACTION_CODE)
        intentAlarmManager.putExtra("type", type)
//        intentAlarmManager.setComponent(
//            ComponentName(
//                MyReceiver::class.java.getPackage().name,
//                MyReceiver::class.java.name
//            )
//        )
        return intentAlarmManager
    }

    fun initAlarmManagerExact() {
        pendingIntentExact = PendingIntent.getBroadcast(context, requestCodeAlarmManagerExact, creatIntentAlarmManager(1), PendingIntent.FLAG_CANCEL_CURRENT)
        startAlarmManagerExact()
    }

    fun startAlarmManagerExact(){
        Log.i(TAG, "startAlarmManagerExact===>>>")
        alarmManager.setExact(
            AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + delayTime, pendingIntentExact)
    }

    fun initAlarmManagerWindow() {
        pendingIntentWindow = PendingIntent.getBroadcast(context, requestCodeAlarmManagerWindow, creatIntentAlarmManager(2), PendingIntent.FLAG_CANCEL_CURRENT)
        startAlarmManagerWindow()
    }

    fun startAlarmManagerWindow(){
        Log.i(TAG, "startAlarmManagerWindow===>>>")
        alarmManager.setWindow(
            AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + delayTime, SystemClock.elapsedRealtime() + 2*delayTime, pendingIntentWindow)
    }

    fun initAlarmManagerAndAllowWhileIdle() {
        pendingIntentAndAllowWhileIdle = PendingIntent.getBroadcast(context, requestCodeAlarmManagerAndAllowWhileIdle, creatIntentAlarmManager(3), PendingIntent.FLAG_CANCEL_CURRENT)
        startAlarmManagerAndAllowWhileIdle()
    }

    fun startAlarmManagerAndAllowWhileIdle(){
        Log.i(TAG, "startAlarmManagerAndAllowWhileIdle===>>>")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setAndAllowWhileIdle(
                AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + delayTime, pendingIntentAndAllowWhileIdle)
        }
    }

    fun initAlarmManagerExactAndAllowWhileIdle() {
        pendingIntentExactAndAllowWhileIdle = PendingIntent.getBroadcast(context, requestCodeAlarmManagerExactAndAllowWhileIdle, creatIntentAlarmManager(4), PendingIntent.FLAG_CANCEL_CURRENT)
        startAlarmManagerExactAndAllowWhileIdle()
    }

    fun startAlarmManagerExactAndAllowWhileIdle(){
        Log.i(TAG, "startAlarmManagerExactAndAllowWhileIdle===>>>")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(
                AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + delayTime, pendingIntentExactAndAllowWhileIdle)
        }
    }
}